#region license
/*
 * Copyright (C) 2007 CchenSoft.com
 * 
 * This library is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU Lesser General Public License 2.1 or later, as
 * published by the Free Software Foundation. See the included License.txt
 * or http://www.gnu.org/copyleft/lesser.html for details.
 * 
 * Author: Billy Zhang
 */
#endregion

using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace CchenSoft.Portal.Util
{
    public class ReflectUtil
    {
        public static T[] GetCustomAttributes<T>(MethodInfo method)
        {
            object[] attrs = method.GetCustomAttributes(typeof(T), false);
            T[] result = new T[attrs.Length];
            for (int i = 0; i < attrs.Length; i++)
                result[i] = (T)attrs[i];
            return result;
        }

        public static object GetPropertyValue(object obj, string propertyName)
        {
            if (!string.IsNullOrEmpty(propertyName))
            {
                PropertyInfo pi = obj.GetType().GetProperty(propertyName);
                if (pi != null)
                    return pi.GetValue(obj, null);
            }
            return null;
        }

        public static void SetPropertyValue(object obj, string propertyName, object propertyValue)
        {
            if (!string.IsNullOrEmpty(propertyName))
            {
                PropertyInfo pi = obj.GetType().GetProperty(propertyName);
                if (pi != null)
                {
                    pi.SetValue(obj, propertyValue, null);
                }
            }

        }
    }
}
